﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Admin;
using NetDon.OA.Interface.Configuration;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public partial class UserCreate : PageBase
    {
        protected Guid UserId
        {
            get
            {
                return Parameters.GetGuid("UserId");
            }
        }
        protected Guid CompanyId
        {
            get
            {
                return Parameters.GetGuid("CompanyId");
            }
        }
        protected Guid DepartId
        {
            get
            {
                return Parameters.GetGuid("DepartId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                CancelButton = btnCancel;

                //txtBirthday.Text = DateTime.Now.ToString(SystemConfiguration.DateFormat);
                txtBirthday_CalendarExtenderBirthday.Format = SystemConfiguration.DateFormat;

                if (UserId != Guid.Empty)
                {
                    //can not modify user type
                    radioUserType.Enabled = false;

                    var service = ServiceContainer.GetService<IUserService>();
                    var entity = service.GetUser(UserId);
                    this.panelUser.DataSource = entity;
                    panelUser.DataBind();
                    radioUserType.SelectedValue = entity.Type.ToString();
                    chlRoles.SelectedValue = entity.RoleId.ToString();
                    if (entity.StoreId.HasValue)
                    {
                        //ddlStore.SelectedValue = entity.StoreId.Value.ToString();
                        //radioUserType.SelectedIndex = 1;
                    }
                    else
                    {
                        radioUserType.SelectedIndex = 0;
                        if (entity.CompanyId.HasValue)
                        {
                            ucCompanyDepartment.CompanyId = entity.CompanyId.Value.ToString();
                        }

                        if (entity.DepartId.HasValue)
                        {
                            ucCompanyDepartment.DepartmentId = entity.DepartId.Value.ToString();
                        }

                        ddlPosition.SelectedValue = entity.PositionId.ToString();

                    }
                    ddlGender.SelectedValue = entity.Gender.ToString();
                    txtBirthday.Text = Convert.ToDateTime(entity.Birthday).ToString(SystemConfiguration.DateFormat);
                }

            }
        }



        protected void radioUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioUserType.SelectedValue == "1")
            {
                //内部员工
     
                //panelUser.Items["Role"].Visible = true;
                panelUser.Items["Company"].Visible = true;
                panelUser.Items["Position"].Visible = true;
            }
            else if (radioUserType.SelectedValue == "2")
            {
                //门店用户
                //panelUser.Items["Role"].Visible = false;
                panelUser.Items["Company"].Visible = false;
                panelUser.Items["Position"].Visible = false;

            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            var userService = ServiceContainer.GetService<IUserService>();

            //DateTime orderDate;
            //if (!DateTime.TryParse(txtBirthday.Text, SystemConfiguration.DateTimeFormatInfo, DateTimeStyles.None,
            //                       out orderDate))
            //{
            //    Alert("请输入正确的生日");
            //    return;
            //}


            //检查日期
            var user = new UserData()
                           {
                               UserId = UserId,
                               UserName = txtUserName.Text.Trim(),
                               FullName = txtFullName.Text.Trim(),
                               Password = txtUserName.Text.Trim(),
                               Type = EnumUtilities.Parse<UserType>(radioUserType.SelectedValue),
                               RoleId = chlRoles.SelectedValue.ToGuid(),
                               NickName = txtNickName.Text.Trim(),
                               Gender = EnumUtilities.Parse<GenderType>(ddlGender.SelectedValue),
                               Mobile = txtMobilePhone.Text.Trim(),
                               Email = txtEmail.Text.Trim(),
                               Address = txtAdress.Text.Trim(),
                               CreateDate = DateTime.Now
                           };
            if (radioUserType.SelectedValue == "1")
            {
                if (string.IsNullOrEmpty(ucCompanyDepartment.CompanyId))
                {
                    Alert("请选择公司！");
                    return;
                }
                //internal employee
                if (!string.IsNullOrEmpty(ucCompanyDepartment.CompanyId))
                user.CompanyId = ucCompanyDepartment.CompanyId.ToGuid();
                if(!string.IsNullOrEmpty(ucCompanyDepartment.DepartmentId))
                user.DepartId = ucCompanyDepartment.DepartmentId.ToGuid();
                if (!string.IsNullOrEmpty(ddlPosition.SelectedValue))
                user.PositionId = ddlPosition.SelectedValue.ToGuid();
            }
            else
            {
                //store user
                //user.StoreId = ddlStore.SelectedValue.ToGuid();
            }
            if (!string.IsNullOrEmpty(txtBirthday.Text.Trim()))
            {
                user.Birthday = Convert.ToDateTime(txtBirthday.Text.Trim());
            }
            bool result = UserId == Guid.Empty ? userService.AddUser(user) : userService.ModifyUser(user);
            if (result)
            {
                Success("保存成功!");
                this.CloseWindow(true);
            }
            else
            {
                ShowDataError(user);
            }
        }
        protected void DateValidator_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            DateTime value;
            e.IsValid = DateTime.TryParse(txtBirthday.Text, SystemConfiguration.DateTimeFormatInfo, DateTimeStyles.None,
                                          out value);
        }


    }
}